package com.stylefeng.guns.rest.modular.App.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.rest.Tools.Result;
import com.stylefeng.guns.rest.common.annotion.SSO;
import com.stylefeng.guns.rest.modular.App.model.LineTimeSlot;
import com.stylefeng.guns.rest.modular.App.service.ILineTimeSlotService;
import com.stylefeng.guns.rest.modular.DTO.LineTImeSlot;
import com.stylefeng.guns.rest.validater.Search;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 列车时刻表
 *
 * @author 张亚林
 * @Date 2018-11-16 18:39
 */
@Controller
@RequestMapping(value = {"/gunsApi/auth/lineTimeSlot"})
@Api(value = "列车时刻表操作相关api", tags = {"列车时刻表操作接口"})
public class LineTimeSlotController extends BaseController {
    @Autowired
    private ILineTimeSlotService lineTimeSlotService;
	
    /**
     * 分页获取列车时刻表列表
     * @param lineTimeSlot
     * @param pageNum
     * @param pageSize
     * @return	
     */
    @ApiOperation(value = "分页获取列车时刻表列表", notes = "分页获取列车时刻表列表", httpMethod = "POST")
    @PostMapping(value = "/pageList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)	
    @ResponseBody
    @SSO
    public Result pageList(
        @RequestBody @ApiParam(name = "lineTimeSlot", value = "列车时刻表对象")@Validated(value = Search.class) LineTImeSlot lineTImeSlot ,
        @ApiParam(name = "pageNum", value = "当前页数", required = true) @RequestParam(value = "pageNum",defaultValue = "1",required = true) Integer pageNum,
        @ApiParam(name = "pageSize", value = "每页显示条数", required = true) @RequestParam(value = "pageSize",defaultValue = "1000", required = true) Integer pageSize){
        try {
            Map map=new HashMap<>();
            Pager pager=new Pager();
            pager.setPageNumber(pageNum);//页码从1 开始
            pager.setPageSize(pageSize);
            String otCode=lineTImeSlot.getOtCode();//线路类型
            if(StringUtils.equals("CJ",otCode)){
                LineTimeSlot lineTimeSlot=new LineTimeSlot();
                BeanUtil.copyProperties(lineTImeSlot,lineTimeSlot,CopyOptions.create().setIgnoreNullValue(true));
                pager.setObj(lineTimeSlot);
                map= this.lineTimeSlotService.pageListLineTimeSlot(pager,getToken());
            }else{
                map.put("rows", new ArrayList<>());
                map.put("total", 0);
                map.put("page", 0);
                map.put("pageCount", 0);
            }
            return Result.success(map);
        }catch(Exception e){
            return Result.error(e.getMessage(),500100);
        }
    }
    /**
     * 通过主键获取列车时刻表对象
     * @param id
     * @return
     */
    @ApiOperation(value = "通过主键获取列车时刻表对象", notes = "通过主键获取列车时刻表对象", httpMethod = "GET")
    @GetMapping(value = "/getById", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @SSO
    public Result getById(@ApiParam(name = "id", value = "主键ID", required = true) @RequestParam(value = "id", required = true) String  id) {
        try {
            return Result.success(lineTimeSlotService.findLineTimeSlotById(id,getToken()));
        }catch(Exception e){
            return Result.error(e.getMessage(),500100);
        }
    }


    /**
     * 获取列车时刻表的Key-Value列表，用于html的Select数据源
     * @param lineTimeSlot
     * @return
     */
    @ApiOperation(value = "获取列车时刻表的Key-Value列表，用于html的Select数据源", notes = "获取列车时刻表的Key-Value列表，用于html的Select数据源", httpMethod = "POST")
    @PostMapping(value = "/optionsList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @SSO
    public Result optionsList(@RequestBody @ApiParam(name = "lineTimeSlot", value = "列车时刻表对象")LineTimeSlot lineTimeSlot) {
        try {
            return Result.success(lineTimeSlotService.optionsLineTimeSlot(lineTimeSlot,getToken()));
        }catch(Exception e){
            return Result.error(e.getMessage(),500100);
        }
    }
	private String getToken(){		
		return getHttpServletRequest().getHeader("Authorization").substring(7);
    }
}
